package com.example.boot.service.admin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.boot.bean.Count;
import com.example.boot.bean.Pull;
import com.example.boot.bean.ReplyInfo;
import com.example.boot.mapper.CountMapper;
import com.example.boot.mapper.PullMapper;
import com.example.boot.mapper.ReplyInfoMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


@SuppressWarnings(value={"unchecked", "rawtypes"})
@Component
public class RabbitMQListen {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    CountMapper countMapper;
    @Autowired
    ReplyInfoMapper replyInfoMapper;
    @Autowired
    PullMapper pullMapper;

    @RabbitListener(queues="boot_queue")
    public void ListenerQueue(ReplyInfo replyInfo){

        //获取user_id

        int userId = replyInfo.getUserId();

        //获取user_name

        String userName = replyInfo.getUserName();

        //获取该user_id下的count值

        QueryWrapper<Count> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Count::getId,userId);
        Integer integer = countMapper.selectCount(wrapper);

        if (integer==0){

            QueryWrapper<Count> wrapper2 = new QueryWrapper<>();
            wrapper2.lambda().eq(Count::getId,userId);
            Count count = new Count();
            count.setId(userId);
            count.setCount(1);
            countMapper.insert(count);

            //如果存在，更新count;

        }if (integer!=0){
            UpdateWrapper<Count> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(Count::getId,userId);
            Count count = countMapper.selectOne(updateWrapper);
            Count count2 = new Count();
            count2.setCount(count.getCount()+1);
            count2.setId(userId);
            countMapper.update(count2,updateWrapper);
        }


        //存user_id
        redisTemplate.opsForZSet().add("Ids01",String.valueOf(userId),replyInfo.getQuestionDate().getTime());
        //存count
        redisTemplate.opsForZSet().incrementScore("count01",String.valueOf(userId),1);
        //存user_name
        redisTemplate.boundValueOps(String.valueOf(userId)).set(userName);

        //存adminId
        int count = pullMapper.selectCount(new QueryWrapper<Pull>().select("DISTINCT user_id").lambda().eq(Pull::getUserId, userId).ne(Pull::getAdminId, 0).eq(Pull::getStatus,0));
        Pull pull = pullMapper.selectOne(new QueryWrapper<Pull>().select("DISTINCT user_id,admin_id").lambda().eq(Pull::getUserId, userId).ne(Pull::getAdminId, 0).eq(Pull::getStatus,0));
            //新问题没被🔒
        if (count==0){
            redisTemplate.opsForZSet().add("noAdmin2",String.valueOf(userId),replyInfo.getQuestionDate().getTime());
        }else {
            //新问题被锁了
            String s = String.valueOf(pull.getAdminId());
            //int adminId = pull.getAdminId();
            redisTemplate.opsForZSet().add(s+"a",String.valueOf(userId),replyInfo.getQuestionDate().getTime());
        }

    }
}